package org.dxy.api.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {
    private int code;
    private T data;
    private String msg;
    private boolean ok;

    public static <T> R<T> ok() {
        return R.<T>builder()
                .code(0)
                .msg("success")
                .ok(true)
                .build();
    }

    public static <T> R<T> ok(T data) {
        return R.<T>builder()
                .code(0)
                .msg("success")
                .data(data)
                .ok(true)
                .build();
    }


    public static <T> R<T> ok(String msg) {
        return R.<T>builder()
                .code(0)
                .msg(msg)
                .ok(true)
                .build();
    }

    public static <T> R<T> ok(T data, String msg) {
        return R.<T>builder()
                .code(200)
                .msg(msg)
                .data(data)
                .ok(true)
                .build();
    }

    public static <T> R<T> fail() {
        return R.<T>builder()
                .code(1)
                .msg("fail")
                .ok(false)
                .build();
    }

    public static <T> R<T> fail(String msg) {
        return R.<T>builder()
                .code(1)
                .msg(msg)
                .ok(false)
                .build();
    }

    public static <T> R<T> fail(int code, String msg) {
        return R.<T>builder()
                .code(code)
                .msg(msg)
                .ok(false)
                .build();
    }


    public static <T> R<T> fail(T data, String msg) {
        return R.<T>builder()
                .code(1)
                .msg(msg)
                .data(data)
                .ok(false)
                .build();
    }
}
